﻿#region Copyright ©2008-2011, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-03-19 13:58:26 +0100 (Thu, 19 Mar 2009) $
// Last Changed Revision $Rev: 42 $
//
// ====================================================================================================
#endregion

using System.IO;
using System.Windows;
using System.Windows.Media;
using Tum.CollabXT.TFS.Properties;
using System.Collections.Generic;
using System.Windows.Controls;
using System;

namespace Tum.CollabXT.TFS
{
    /// <summary>
    /// Interaction logic for TFSConfigFrame.xaml
    /// </summary>
    public partial class TFSConfigFrame : ToolProviderConfigPage
    {
        TFSToolProvider ToolProvider;
        IConversionWorkflow ConversionWorkflow;

        static readonly string[] MicroWorkflowPages = new[] { "TFSTemplatePage.xaml", "TFSUploadsPage.xaml", "TFSOutputPage.xaml" };
        static int MicroWorkflowPage = 0;


        public TFSConfigFrame()
        {
            InitializeComponent();

            //Set subpage handler and load first subpage
            FrameSubPages.NavigationService.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(NavigationService_LoadCompleted);
            LoadCurrentWorkflowPage();
        }

        void NavigationService_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            //Send provider and workflow handles to subpage
            var subpage = e.Content as TFSPage;
            if(subpage != null)
                subpage.ReceiveHandles(ToolProvider, ConversionWorkflow);
        }

        public override void ReceiveProviderHandle(IToolProvider toolProvider)
        {
            ToolProvider = toolProvider as TFSToolProvider;
        }

        public override void ReceiveWorkflowHandle(IConversionWorkflow workflow)
        {
            ConversionWorkflow = workflow;
        }

        public override bool PrevPageRequested()
        {
            var subpage = FrameSubPages.Content as TFSPage;
            if (subpage != null)
            {
                if (!subpage.ChangePage(false))
                    return false;
            }

            //Move to previous page
            if (MicroWorkflowPage <= 0)
                return base.PrevPageRequested();
            else
            {
                MicroWorkflowPage--;
                LoadCurrentWorkflowPage();
                return false;
            }
        }

        public override bool NextPageRequested()
        {
            var subpage = FrameSubPages.Content as TFSPage;
            if (subpage != null)
            {
                if (!subpage.ChangePage(true))
                    return false;
            }

            //Move to next page
            if (MicroWorkflowPage + 1 >= MicroWorkflowPages.Length)
                return base.NextPageRequested();
            else
            {
                MicroWorkflowPage++;
                LoadCurrentWorkflowPage();
                return false;
            }
        }

        private void LoadCurrentWorkflowPage()
        {
            FrameSubPages.Source = new Uri(MicroWorkflowPages[MicroWorkflowPage], System.UriKind.Relative);
        }
    }
}
